import { FileIoUtil } from "./FileIOUtil";
import { Main } from "../Main";
import { ConfigUtil } from "./ConfigUtil";

/**
 * 更改lua代码工具，比如设置是否使用sdk
 * @author 赵占涛
 */
export class ChangeLuaCodeUtil {
    static exec(plat:string, cb:()=>void) {
        console.log("ChangeLuaCodeUtil");
        let configLuaPath = Main.codePath + "Resources/src/config.lua";
        FileIoUtil.readFile(configLuaPath, (data:Buffer)=>{
            let dataStr = data.toString();
            let isOpenSdk = plat == "dev" ? 0 : 1;
            dataStr = dataStr.replace(`isOpenSdk = ${1-isOpenSdk}`, `isOpenSdk = ${isOpenSdk}`);
            console.log("datastr", dataStr);
            FileIoUtil.writeFile(configLuaPath, dataStr, ()=>{
                let p000 = Main.codePath + "Resources/src/app/config/platCfg/p000.lua";
                FileIoUtil.readFile(p000, (data:Buffer)=>{
                    let dataStr = data.toString();
                    let defaultLanguage = ConfigUtil.PlatCfg[plat].defaultLanguage;
                    dataStr = dataStr.replace(`["language"]="cn"`, `["language"]="${defaultLanguage}"`);
                    let languageSet = "";
                    for (const key in ConfigUtil.PlatCfg[plat].language) {
                        if (ConfigUtil.PlatCfg[plat].language.hasOwnProperty(key)) {
                            languageSet += `"${key}",`
                        }
                    }
                    dataStr = dataStr.replace(/languageSet[\s\S]*},/, `languageSet"]={${languageSet}}`);
                    console.log("datastr", dataStr);
                    FileIoUtil.writeFile(p000, dataStr, cb);
                });
            });
        });
    }
}